#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <tchar.h>
#include <cstdio>
#include <string>
#include <WinSock2.h>
#include <Windows.h>
#include <WS2tcpip.h>
using namespace std;
//#pragma warning(disable:4996)
#pragma comment(lib, "ws2_32.lib")

uint16_t serverport = 8888;
string serverip = "159.75.96.122";

int main() {
	cout << "hello world" << endl;
    WSAData wsd;
	WSAStartup(MAKEWORD(2, 2), &wsd);
	
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);//要保证端口号是网络字节序列，因为要发给对方。htons主机序列转网络序列
    //server.sin_addr.s_addr = inet_addr(serverip.c_str());
    inet_pton(AF_INET, serverip.c_str(), &server.sin_addr);

    SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        cout << "socker error" << endl;
        return 1;
    }

    string message;
    char buffer[1024];
    while (true) {
        cout << "Please Enter@ ";
        getline(cin, message);
        // cout << message << endl;
        sendto(sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&server, sizeof(server));

        struct sockaddr_in tmp;
        int len = sizeof(tmp);
        int s = recvfrom(sockfd, buffer, 1023, 0, (struct sockaddr*)&tmp, &len);
        if (s > 0) {
            buffer[s] = 0;
            cout << buffer << endl;
        }
    }

    closesocket(sockfd);
	WSACleanup();
	return 0;
}